package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/set")
@Slf4j
public class SetRedisController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping
    public void execute() {
        SetOperations operations = stringRedisTemplate.opsForSet();
        operations.add("A", "aa", "kk", "mm", "aa", "ii", "bb", "ll", "zz", "jj", "ff");
        long size = operations.size("A");
        log.info("size:{}", size);
        Set<String> set1 = operations.members("A");
        log.info("set1:{}", set1);
        Object v1 = operations.randomMember("A");
        List<String> v2 = operations.randomMembers("A", 3);
        Set<String> v3 = operations.distinctRandomMembers("A", 3);
        log.info("v1:{},v2:{},v3:{}", v1, v2, v3);
        operations.add("B1", "aa", "bb", "cc");
        operations.add("B2", "aa", "cc", "kk");
        Set<String> v4 = operations.difference("B1", "B2");
        Set<String> v5 = operations.union("B1", "B2");
        Set<String> v6 = operations.intersect("B1", "B2");
        Set<String> v7 = operations.intersect("B2", "B1");
        log.info("v4:{},v5:{},v6:{},v7:{}", v4, v5, v6, v7);
    }
}
